package com.wct.mall.config;

import com.wct.mall.common.HttpInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.List;


/**
 * @author WEI
 * @date 2020/3/22
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    /**
     * json解析
     */
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
    }

    /**
     * http 请求拦截器
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HttpInterceptor());
    }

    /**
     * 全局异常处理
     */
    @Override
    public HandlerExceptionResolver handlerExceptionResolver(ContentNegotiationManager contentNegotiationManager) {
        return super.handlerExceptionResolver(contentNegotiationManager);
//        return new SpringExceptionResolver();
    }

    /**
     * 视图解析器
     */
    @Override
    public ViewResolver mvcViewResolver(ContentNegotiationManager contentNegotiationManager) {
//        return super.mvcViewResolver(contentNegotiationManager);
        return new InternalResourceViewResolver();
    }

    /**
     * 视图映射
     */
    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }

    /**
     * 跨域请求
     */
    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**");
    }
}
